"""add cycle day logs

Revision ID: 202510170002
Revises: 202510170001
Create Date: 2025-10-17 18:55:00
"""

from alembic import op
import sqlalchemy as sa


revision = "202510170002"
down_revision = "202510170001"
branch_labels = None
depends_on = None


def upgrade() -> None:
  flow_enum = sa.Enum("none", "light", "medium", "heavy", name="cycle_flow_level")
  flow_enum.create(op.get_bind(), checkfirst=True)

  op.create_table(
    "cycle_day_logs",
    sa.Column("id", sa.Integer(), primary_key=True, index=True),
    sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    sa.Column("user_id", sa.Integer(), nullable=False),
    sa.Column("relationship_id", sa.Integer(), nullable=True),
    sa.Column("date", sa.Date(), nullable=False),
    sa.Column("flow_level", flow_enum, nullable=False, server_default="none"),
    sa.Column("symptoms_json", sa.JSON(), nullable=True),
    sa.Column("mood", sa.String(length=64), nullable=True),
    sa.Column("temperature", sa.Float(), nullable=True),
    sa.ForeignKeyConstraint(["user_id"], ["users.id"], ondelete="CASCADE"),
    sa.ForeignKeyConstraint(["relationship_id"], ["relationships.id"], ondelete="SET NULL"),
    sa.UniqueConstraint("user_id", "date", name="uq_cycle_day_user_date"),
  )
  op.create_index("ix_cycle_day_logs_user_id", "cycle_day_logs", ["user_id"], unique=False)
  op.create_index("ix_cycle_day_logs_relationship_id", "cycle_day_logs", ["relationship_id"], unique=False)


def downgrade() -> None:
  op.drop_index("ix_cycle_day_logs_relationship_id", table_name="cycle_day_logs")
  op.drop_index("ix_cycle_day_logs_user_id", table_name="cycle_day_logs")
  op.drop_table("cycle_day_logs")
  try:
    sa.Enum(name="cycle_flow_level").drop(op.get_bind(), checkfirst=True)
  except Exception:
    pass

